package cn.fl.ref.mrgnmanageinfo.controller;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.fncprjbscinfo.service.IFncPrjBscInfoService;
import cn.fl.ref.fncprjbscinfo.vo.FncPrjBscInfoVO;
import cn.fl.ref.fncprjpaysch.vo.FncPrjPaySchMVO;
import cn.fl.ref.fnovercollinfo.controller.FncOverCollInfoController;
import cn.fl.ref.fribaseinfo.service.IFriBaseInfoService;
import cn.fl.ref.fribaseinfo.transform.BankAccInfoVOTransform;
import cn.fl.ref.fribaseinfo.vo.BankAccInfoVO;
import cn.fl.ref.fribaseinfo.vo.FriBaseInfoVO;
import cn.fl.ref.mrgnmanageinfo.service.IMrgnManageInfoService;
import cn.fl.ref.mrgnmanageinfo.transform.MrgnManageInfoVOTransform;
import cn.fl.ref.mrgnmanageinfo.vo.MrgnDetailInfoVO;
import cn.fl.ref.mrgnmanageinfo.vo.MrgnManageInfoVO;
import cn.fl.ref.mrgnmanageinfo.vo.MrgnTkOffInfoVO;
import cn.fl.system.condition.SysBkAccInfoQueryCondition;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysBkAccInfoService;
import cn.fl.system.vo.SysBkAccInfoVO;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import fl.flow.facade.flow.constant.FlowConstant;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;
import fl.flow.facade.flow.dto.FlowStartedDTO;
import fl.ref.fribaseinfo.condition.BankAccInfoQueryCondition;
import fl.ref.fribaseinfo.condition.FriBaseInfoQueryCondition;
import fl.ref.fribaseinfo.dto.BankAccInfoDTO;
import fl.ref.fribaseinfo.service.IBankAccInfoApiService;
import fl.ref.mrgnmanageinfo.condition.MrgnManageInfoQueryCondition;
import fl.ref.mrgnmanageinfo.constant.MrgnManageInfoConstant;
import fl.ref.mrgnmanageinfo.dto.MrgnManageInfoDTO;


/**
 * @Description: 保证金基础信息
 */
@Controller
@RequestMapping("/rfbase/mrgnmanageinfo")
public class MrgnManageInfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(MrgnManageInfoController.class);

	@Autowired
	IMrgnManageInfoService mrgnManageInfoService;
	
	@Autowired
	private IFriBaseInfoService friBaseInfoService;
	
	@Autowired
	private IFncPrjBscInfoService fncPrjBscInfoService;

	
	/**
	 * 融资机构银行帐号信息
	 */
	@Autowired
	private IBankAccInfoApiService bankAccInfoApiService;
	
	/**
	 * 狮桥支付银行账户
	 */
	@Autowired
	private ISysBkAccInfoService sysBkAccInfoService;
	
	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		
		logger.info("加载mrgnmanageinfo列表页面.");
		ModelAndView view = new ModelAndView("/rfbase/mrgnmanageinfo/mrgnManageList");
		FriBaseInfoQueryCondition qc = new FriBaseInfoQueryCondition();
		qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
		qc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
		List<FriBaseInfoVO> friBaseInfoVOList=this.friBaseInfoService.searchList(qc);
		List<KeyValueVO> friBaseInfoList=this.getKvListT(friBaseInfoVOList,"id","fundRasInsNm");
		view.addObject("friBaseInfoList", friBaseInfoList);
		List<FriBaseInfoVO>  friList = null;
		try {
			FriBaseInfoQueryCondition friqc = new FriBaseInfoQueryCondition();
//			friqc.setInsUseCd("1");
			friList=friBaseInfoService.searchList(friqc);
		} catch (Exception e) {
			BaseLogUtils.error(logger, "toListPage", "异常"+e.getMessage(), e);
		}
		view.addObject("frilist",JSON.toJSONString(friList));
		return view;
	}
	
	/**
	 * 初始化保证金列表
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String fncInsId= request.getParameter("fncInsId");
		String fncPrjNm= request.getParameter("fncPrjNm");
		String payTmMin= request.getParameter("payTmMin");
		String payTmMax= request.getParameter("payTmMax");
	
		
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		MrgnManageInfoQueryCondition qc=new MrgnManageInfoQueryCondition();
		
		if (StringUtils.isNotEmpty(fncInsId)) {
			qc.setFncInsId(Long.valueOf(fncInsId));
		}
		if (StringUtils.isNotEmpty(fncPrjNm)) {
			qc.setFncPrjNm(fncPrjNm);
		}
		if (StringUtils.isNotEmpty(payTmMin)) {
			Date s = DateUtils.formatStringToDate(payTmMin, DateUtils.YYYY_MM_DD);
			qc.setPayTmMin(s);
		}
		if (StringUtils.isNotEmpty(payTmMax)) {
			Date s = DateUtils.formatStringToDate(payTmMax, DateUtils.YYYY_MM_DD);
			qc.setPayTmMax(s);
		}
		
		page.setCondition(qc);
		
		Pager pager = this.mrgnManageInfoService.searchListPage(page);
		List<MrgnManageInfoDTO> listDTO = (List<MrgnManageInfoDTO>)pager.getResultList();
		List<MrgnManageInfoVO> list=MrgnManageInfoVOTransform.toVOList(listDTO);
		setShowColor(list);
		ListPageVO<MrgnManageInfoVO> listPage = new ListPageVO<MrgnManageInfoVO>(list, pager.getTotalCount());

		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}
	
	/**
	 *  超期状态提醒,颜色显示
	 *  i.	绿色：当前时间——计划退回时间>7天；
		ii.	黄色：当前时间——计划退回时间<=7天；
		iii.	红色：当前时间>=计划退回时间（天）；
	 * @param mrgnManageInfoVOList
	 */
	private void setShowColor(List<MrgnManageInfoVO> mrgnManageInfoVOList) {
		if(mrgnManageInfoVOList != null) {
			long nowDate = new Date().getTime();//当前日期
			for (MrgnManageInfoVO mrgnManageInfoVO : mrgnManageInfoVOList) {
				if(mrgnManageInfoVO.getReReturnTm() != null) {
					
					long betweenDays;
					try {
						betweenDays = FncOverCollInfoController.daysBetween(new Date(),mrgnManageInfoVO.getReReturnTm());
						if(betweenDays > 7) {
							mrgnManageInfoVO.setShowColor("green");
						}else if(betweenDays >0 && betweenDays <= 7) {
							mrgnManageInfoVO.setShowColor("yellow");
						}else {
							mrgnManageInfoVO.setShowColor("red");
						}
						BaseLogUtils.info(logger, "setShowColor","betweenDays:" + betweenDays,CurrentThreadContext.getCurrentUserName());
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toMrgnDetailInfoPage.do", method = { RequestMethod.GET})
	public ModelAndView toMrgnDetailInfoPage(HttpServletRequest request, HttpServletResponse response) {
		
		logger.info("加载mrgnmanageinfo列表页面.");
		String lgcId= request.getParameter("lgcId");
		ModelAndView view = new ModelAndView("/rfbase/mrgnmanageinfo/mrgnManageDetail");
		view.addObject("lgcId", lgcId);
		return view;
	}
	
	
	/**
	 * 保证金详情页面初始化
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/mrgnDetailInfoinit.do", method = { RequestMethod.POST})
	@ResponseBody
	public DataResultVO mrgnDetailInfoinit(HttpServletRequest request, HttpServletResponse response) {
		String lgcId = request.getParameter("lgcId");
		if (StringUtils.isEmpty(lgcId)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "项目信息编号不能为空!");
		}
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "获取成功!");
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			MrgnManageInfoVO mrgnManageInfo = this.mrgnManageInfoService.selectPrjInfoByPrjId(Long.valueOf(lgcId));
			List<MrgnDetailInfoVO> mrgnReturnInfoBf = this.mrgnManageInfoService.selectReturnInfoByPrjId(Long.valueOf(lgcId));
			List<MrgnTkOffInfoVO> mrgnDeductInfo = this.mrgnManageInfoService.selectDeductInfoByPrjId(Long.valueOf(lgcId));
			List<MrgnDetailInfoVO> mrgnReturnInfo=new ArrayList<MrgnDetailInfoVO>();
			if(mrgnReturnInfoBf.size()>0){
				// 保证金收款帐户信息
				for(int i=0;i<mrgnReturnInfoBf.size();i++){
				   MrgnDetailInfoVO mrgnDetailInfoVO=this.getBankInfo(mrgnReturnInfoBf.get(i));
				   mrgnReturnInfo.add(mrgnDetailInfoVO);
				}
			}
			mrgnManageInfo.setMrgnDetailInfoList(mrgnReturnInfo);
			mrgnManageInfo.setMrgnTkOffInfoList(mrgnDeductInfo);
			map.put("mrgnManageInfo", mrgnManageInfo);
			result.setData(map);
		}catch (BaseException ex) {
			ex.printStackTrace();
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "mrgnDetailInfoinit", "获取数据失败,项目逻辑编号["+lgcId+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			ex.printStackTrace();
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误!");
			BaseLogUtils.error(logger, "mrgnDetailInfoinit", "获取数据失败,项目逻辑编号["+lgcId+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	/**
	 * 保证金退回列表页面
	 */
	@RequestMapping(value = "/toReturnOprtPage.do", method = { RequestMethod.GET})
	public ModelAndView toReturnOprtPage(HttpServletRequest request, HttpServletResponse response) {
		
		logger.info("加载保证金退回列表页面.");
		ModelAndView view = new ModelAndView("/rfbase/mrgnmanageinfo/mrgnReturnOprt");
		String prjLgcSn = request.getParameter("lgcId");
		if (StringUtils.isEmpty(prjLgcSn)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "项目信息编号不能为空!");
		}
		FncPrjBscInfoVO fncPrjBscInfoVO =this.fncPrjBscInfoService.selectById(Long.valueOf(prjLgcSn));
		String fncInsId=fncPrjBscInfoVO.getFncinsid();
		view.addObject("fncInsId", fncInsId);
		view.addObject("prjLgcSn", prjLgcSn);
		return view;
	}
	
	/**
	 * 保证金退回添加-页面初始化
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getMrgnReturnInfoByLgcId.do", method = { RequestMethod.POST})
	@ResponseBody
	public DataResultVO getMrgnReturnInfoByLgcId(HttpServletRequest request, HttpServletResponse response) {
		String prjLgcSn = request.getParameter("prjLgcSn");
		Map<String, Object> map=new HashMap<String, Object>();
		if (StringUtils.isEmpty(prjLgcSn)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "项目信息编号不能为空!");
		}
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "获取成功!");
		try {
			MrgnManageInfoVO mrgnManageInfo = this.mrgnManageInfoService.selectPrjInfoByPrjId(Long.valueOf(prjLgcSn));
			List<MrgnDetailInfoVO> mrgnReturnInfoBf = this.mrgnManageInfoService.selectReturnInfoByPrjId(Long.valueOf(prjLgcSn));
			List<MrgnDetailInfoVO> mrgnReturnInfo=new ArrayList<MrgnDetailInfoVO>();
			if(mrgnReturnInfoBf.size()>0){
				// 保证金收款帐户信息
				for(int i=0;i<mrgnReturnInfoBf.size();i++){
				   MrgnDetailInfoVO mrgnDetailInfoVO=this.getBankInfo(mrgnReturnInfoBf.get(i));
				   mrgnReturnInfo.add(mrgnDetailInfoVO);
				}
			}
			mrgnManageInfo.setMrgnDetailInfoList(mrgnReturnInfo);
			map.put("mrgnManageInfo", mrgnManageInfo);
			result.setData(map);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getMrgnReturnInfoByLgcId", "获取数据失败,项目逻辑编号["+prjLgcSn+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误!");
			BaseLogUtils.error(logger, "getMrgnReturnInfoByLgcId", "获取数据失败,项目逻辑编号["+prjLgcSn+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	/**
	 * 新增保证金退回信息
	 */
	@RequestMapping(value = "/mrgnReturnAdd.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		String prjLgcSn = request.getParameter("prjLgcSn");
		String srplsAtm = request.getParameter("srplsAtm");
		String txnAmt = request.getParameter("txnAmt");
		String txnTm = request.getParameter("txnTm");
		String recvAcc = request.getParameter("recvAcc");
		String payAcc = request.getParameter("payAcc");
		
		MrgnDetailInfoVO entity=new MrgnDetailInfoVO();
		if (StringUtils.isNotEmpty(prjLgcSn)) {
			entity.setPrjLgcSn(Long.valueOf(prjLgcSn));
		}
		if (StringUtils.isNotEmpty(recvAcc)) {
			entity.setRecvAcc(Long.valueOf(recvAcc));
		}
		if (StringUtils.isNotEmpty(payAcc)) {
			entity.setPayAcc(Long.valueOf(payAcc));
		}
		if (StringUtils.isNotEmpty(srplsAtm)) {
			entity.setSrplsAtm(new BigDecimal(srplsAtm));
		}
		if (StringUtils.isNotEmpty(txnAmt)) {
			entity.setTxnAmt(new BigDecimal(txnAmt));
		}
		if (StringUtils.isNotEmpty(txnTm)) {
			Date s = DateUtils.formatStringToDate(txnTm, DateUtils.YYYY_MM_DD);
			entity.setTxnTm(s);
		}
		entity.setTxnTp(MrgnManageInfoConstant.MRGN_MGT_INFO_TXN_TP_RETURN);
		entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
		entity.setCrtTm(new Date());
		entity.setIsValid(BaseConstant.IS_YESNO_NO); // 无效
		entity.setIsDel(BaseConstant.IS_YESNO_NO); // 未删除
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "保证金退回信息添加成功!");
		
		try {
			Long mainId = this.mrgnManageInfoService.addMrgnDetail(entity);
			result.setData(mainId);
			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setInfo("保证金退回信息添加成功!");
			BaseLogUtils.info(logger, "mrgnReturnAdd", "新增数据 ,项目逻辑序号:id["+prjLgcSn+"]. ID:"+mainId, CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "mrgnReturnAdd", "新增数据失败,项目逻辑序号:id["+prjLgcSn+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("新增失败!");
			BaseLogUtils.error(logger, "mrgnReturnAdd", "新增数据失败,项目逻辑序号:id["+prjLgcSn+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	
	/**
	 * 保证金退回-修改-页面
	 */
	@RequestMapping(value = "/toReturnOprtUpdatePage.do", method = { RequestMethod.GET})
	public ModelAndView toReturnOprtUpdatePage(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView view = new ModelAndView("/rfbase/mrgnmanageinfo/mrgnReturnOprt");
		String lgcId = request.getParameter("lgcId");//保证金流水逻辑编号
		if (StringUtils.isEmpty(lgcId)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "保证金逻辑编号不能为空!");
		}
		MrgnManageInfoVO mrgnManageInfo = this.mrgnManageInfoService.selectPrjInfoByMgtLgcSn(Long.valueOf(lgcId));
		Long fncInsId=0L;
		if(mrgnManageInfo!=null)
			fncInsId=mrgnManageInfo.getFncInsId();
		else
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "查询不到融资机构编号不能为空!");
		view.addObject("fncInsId", fncInsId); 
		view.addObject("lgcId", lgcId);
		view.addObject("mrgnManageInfo", mrgnManageInfo);
		return view;
	}
	
	/**
	 * 保证金退回-修改
	 */
	@RequestMapping(value = "/mrgnReturnUpdate.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object mrgnReturnUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		String lgcId = request.getParameter("lgcId");
		String prjLgcSn = request.getParameter("prjLgcSn");
		String srplsAtm = request.getParameter("srplsAtm");
		String txnAmt = request.getParameter("txnAmt");
		String txnTm = request.getParameter("txnTm");
		String recvAcc = request.getParameter("recvAcc");
		String payAcc = request.getParameter("payAcc");
		
		MrgnDetailInfoVO entity=new MrgnDetailInfoVO();
		if (StringUtils.isNotEmpty(lgcId)) {
			entity.setId(Long.valueOf(lgcId));
		}
		if (StringUtils.isNotEmpty(prjLgcSn)) {
			entity.setPrjLgcSn(Long.valueOf(prjLgcSn));
		}
		if (StringUtils.isNotEmpty(recvAcc)) {
			entity.setRecvAcc(Long.valueOf(recvAcc));
		}
		if (StringUtils.isNotEmpty(payAcc)) {
			entity.setPayAcc(Long.valueOf(payAcc));
		}
		if (StringUtils.isNotEmpty(srplsAtm)) {
			entity.setSrplsAtm(new BigDecimal(srplsAtm));
		}
		if (StringUtils.isNotEmpty(txnAmt)) {
			entity.setTxnAmt(new BigDecimal(txnAmt));
		}
		if (StringUtils.isNotEmpty(txnTm)) {
			Date s = DateUtils.formatStringToDate(txnTm, DateUtils.YYYY_MM_DD);
			entity.setTxnTm(s);
		}
		entity.setTxnTp(MrgnManageInfoConstant.MRGN_MGT_INFO_TXN_TP_RETURN);
		entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
		entity.setMdfTm(new Date());
		entity.setIsValid(BaseConstant.IS_YESNO_NO); // 有效
		entity.setIsDel(BaseConstant.IS_YESNO_NO); // 未删除
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "保证金退回信息修改成功!");
		
		try {
			this.mrgnManageInfoService.updateMrgnDetail(entity);
			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setInfo("保证金退回信息修改成功!");
			BaseLogUtils.info(logger, "mrgnReturnUpdate", "修改数据,id["+lgcId+"].", CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "mrgnReturnUpdate", "修改数据失败,id["+lgcId+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误");
			BaseLogUtils.error(logger, "mrgnReturnUpdate", "修改数据失败,id["+lgcId+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	/**
	 * 保证金退回到帐-详情
	 */
	@RequestMapping(value = "/toReturnOprtDetailPage.do", method = { RequestMethod.GET})
	public ModelAndView toReturnOprtDetailPage(HttpServletRequest request, HttpServletResponse response) {
		
		logger.info("加载保证金退回详情页面.");
		ModelAndView view = new ModelAndView("/rfbase/mrgnmanageinfo/mrgnReturnOprtDetail");
		String lgcId = request.getParameter("lgcId");//保证金流水逻辑编号
		if (StringUtils.isEmpty(lgcId)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "保证金交易信息逻辑编号不能为空!");
		}
		MrgnManageInfoVO mrgnManageInfo = this.mrgnManageInfoService.selectPrjInfoByMgtLgcSn(Long.valueOf(lgcId));
		Long fncInsId=0L;
		if(mrgnManageInfo!=null)
			fncInsId=mrgnManageInfo.getFncInsId();
		else
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "查询不到融资机构编号!");
		view.addObject("fncInsId", fncInsId); 
		view.addObject("lgcId", lgcId);
		return view;
	}
	
	/**
	 * 保证金退回修改-详情页面初始化
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getMrgnReturnInfoByMgrnLgcId.do", method = { RequestMethod.POST})
	@ResponseBody
	public DataResultVO getMrgnReturnInfoByMgrnLgcId(HttpServletRequest request, HttpServletResponse response) {
		String lgcId = request.getParameter("lgcId");
		if (StringUtils.isEmpty(lgcId)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "保证金逻辑编号不能为空!");
		}
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "获取成功!");
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			MrgnManageInfoVO mrgnManageInfo = this.mrgnManageInfoService.selectPrjInfoByMgtLgcSn(Long.valueOf(lgcId));
			Long prjId=mrgnManageInfo.getId();
			if(prjId==null)
			   throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "查询不到保证金项目的项目编号!");
			List<MrgnDetailInfoVO> mrgnReturnInfoListBf = this.mrgnManageInfoService.selectReturnInfoByPrjId(Long.valueOf(prjId));
			MrgnDetailInfoVO mrgnReturnInfoBf= this.mrgnManageInfoService.selectCheckMrgnInfoById(Long.valueOf(lgcId));
			List<MrgnDetailInfoVO> mrgnReturnInfoList=new ArrayList<MrgnDetailInfoVO>();
			if(mrgnReturnInfoListBf.size()>0){
				// 保证金收款帐户信息
				for(int i=0;i<mrgnReturnInfoListBf.size();i++){
				   MrgnDetailInfoVO mrgnDetailInfoVO=this.getBankInfo(mrgnReturnInfoListBf.get(i));
				   mrgnReturnInfoList.add(mrgnDetailInfoVO);
				}
			}
			MrgnDetailInfoVO mrgnReturnInfo=this.getBankInfo(mrgnReturnInfoBf);
			mrgnManageInfo.setMrgnDetailInfoList(mrgnReturnInfoList);
			map.put("mrgnManageInfo", mrgnManageInfo);
			map.put("mrgnReturnInfo", mrgnReturnInfo);
			result.setData(map);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getMrgnReturnInfoByLgcId", "获取数据失败,保证金逻辑编号["+lgcId+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误!");
			BaseLogUtils.error(logger, "getMrgnReturnInfoByLgcId", "获取数据失败,保证金逻辑编号["+lgcId+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
  
	/**
	 * 保证金坐扣-添加页面
	 */
	@RequestMapping(value = "/toDeductOprtPage.do", method = { RequestMethod.GET})
	public ModelAndView toDeductOprtPage(HttpServletRequest request, HttpServletResponse response) {
		
		logger.info("加载保证金坐扣页面.");
		ModelAndView view = new ModelAndView("/rfbase/mrgnmanageinfo/mrgnDeductOprt");
		String prjLgcSn = request.getParameter("prjLgcId");//项目逻辑编号
		if (StringUtils.isEmpty(prjLgcSn)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "项目信息编号不能为空!");
		}
		view.addObject("prjLgcSn", prjLgcSn);
		return view;
	}
	
	/**
	 * 保证金坐扣-修改页面
	 */
	@RequestMapping(value = "/toDeductOprtUpdatePage.do", method = { RequestMethod.GET})
	public ModelAndView toDeductOprtUpdatePage(HttpServletRequest request, HttpServletResponse response) {
		
		logger.info("加载保证金坐扣页面.");
		ModelAndView view = new ModelAndView("/rfbase/mrgnmanageinfo/mrgnDeductOprt");
		String lgcId = request.getParameter("lgcId");//保证金流水信息的逻辑序号
		if (StringUtils.isEmpty(lgcId)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "保证金流水逻辑序号不能为空!");
		}
		MrgnDetailInfoVO mrgnDetailInfo=this.mrgnManageInfoService.selectMrgnDetailInfo(Long.valueOf(lgcId));
		Long prjLgcSn=mrgnDetailInfo.getPrjLgcSn();
		view.addObject("lgcId", lgcId);
		view.addObject("prjLgcSn", prjLgcSn);
		return view;
	}
	
	/**
	 * 保证金坐扣-新增-修改-页面初始化
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getMrgnDeductInfoByLgcId.do", method = { RequestMethod.POST})
	@ResponseBody
	public DataResultVO getMrgnDeductInfoByLgcId(HttpServletRequest request, HttpServletResponse response) {
		String prjLgcSn = request.getParameter("prjLgcSn");//项目信息编号
		String lgcIdStr = request.getParameter("lgcId");
		Long lgcId=null;
		if (StringUtils.isEmpty(prjLgcSn)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "项目信息编号不能为空!");
		}
		if (StringUtils.isNotEmpty(lgcIdStr)) {
			lgcId=Long.valueOf(lgcIdStr);
		}
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "获取成功!");
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			List<MrgnTkOffInfoVO> mrgnTkOffInfoList =new ArrayList<MrgnTkOffInfoVO>();
			MrgnManageInfoVO mrgnManageInfo = this.mrgnManageInfoService.selectPrjInfoByPrjId(Long.valueOf(prjLgcSn));
			List<MrgnTkOffInfoVO> mrgnDeductInfo = this.mrgnManageInfoService.selectDeductInfoByPrjId(Long.valueOf(prjLgcSn));
			mrgnManageInfo.setMrgnTkOffInfoList(mrgnDeductInfo);
			if(lgcId==null){
				mrgnTkOffInfoList = this.mrgnManageInfoService.getPrjPaySchInfoAdd(Long.valueOf(prjLgcSn));
			}else{
				mrgnTkOffInfoList = this.mrgnManageInfoService.getPrjPaySchInfoUpdate(lgcId);
			}
			map.put("mrgnManageInfo", mrgnManageInfo);
			map.put("mrgnTkOffInfoList", mrgnTkOffInfoList);
			result.setData(map);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getMrgnDeductInfoByLgcId", "获取数据失败,项目逻辑编号["+lgcId+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误!");
			BaseLogUtils.error(logger, "getMrgnDeductInfoByLgcId", "获取数据失败,项目逻辑编号["+lgcId+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	/**
	 * 保证金坐扣-详情页面
	 */
	@RequestMapping(value = "/toMrgnDeductOprtDetail.do", method = { RequestMethod.GET})
	public ModelAndView toMrgnDeductOprtDetail(HttpServletRequest request, HttpServletResponse response) {
		
		logger.info("加载保证金坐扣页面.");
		ModelAndView view = new ModelAndView("/rfbase/mrgnmanageinfo/mrgnDeductOprtDetail");
		String lgcId = request.getParameter("lgcId");//项目逻辑编号
		if (StringUtils.isEmpty(lgcId)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "项目信息编号不能为空!");
		}
		MrgnDetailInfoVO mrgnDetailInfo=this.mrgnManageInfoService.selectMrgnDetailInfo(Long.valueOf(lgcId));
		Long prjLgcSn=mrgnDetailInfo.getPrjLgcSn();
		view.addObject("lgcId", lgcId);
		view.addObject("prjLgcSn", prjLgcSn);
		return view;
	}
	
	/**
	 * 保证金坐扣-详情页面初始化
	 * 根据保证金流水信息的逻辑序号ID查询还款表信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getMrgnDeductInfoById.do", method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public DataResultVO getMrgnDeductInfoById(HttpServletRequest request, HttpServletResponse response) {
		
		String lgcId = request.getParameter("lgcId");//保证金流水信息的逻辑序号
		
		if (StringUtils.isEmpty(lgcId)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "保证金流水信息的逻辑序号不能为空!");
		}
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "获取成功!");
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			MrgnManageInfoVO mrgnPayingInfo=this.mrgnManageInfoService.selectPrjInfoByMgtLgcSn(Long.valueOf(lgcId));
			List<MrgnTkOffInfoVO> mrgnDeductInfoList = this.mrgnManageInfoService.getPrjPaySchInfoById(Long.valueOf(lgcId));
			if(mrgnDeductInfoList!=null)
			  mrgnPayingInfo.setMrgnTkOffInfoList(mrgnDeductInfoList);
			Long prjLgcSn=mrgnPayingInfo.getId();
			List<MrgnTkOffInfoVO> mrgnDeductedInfo = this.mrgnManageInfoService.selectDeductInfoByPrjId(prjLgcSn);
			map.put("mrgnDeductInfo", mrgnDeductedInfo);
			map.put("mrgnPayingInfo", mrgnPayingInfo); //项目信息加还款信息
			result.setData(map);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getMrgnCheckInfoById", "获取还款表数据失败."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误!");
			BaseLogUtils.error(logger, "getMrgnCheckInfoById", "获取还款表数据失败."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	

	/**
	 * 保证金坐扣-新增
	 */
	@RequestMapping(value = "/addDeductOprt.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object addDeductOprt(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		String txnAmt = request.getParameter("txnAmt"); 
	    String srplsAtm = request.getParameter("srplsAtm"); 
	    String prjLgcSn = request.getParameter("prjLgcSn"); 
		String deductOprtArray = request.getParameter("deductOprtArray"); 
		
	    //添加保证金流水信息
		MrgnDetailInfoVO entity=new MrgnDetailInfoVO();
		if (StringUtils.isNotEmpty(prjLgcSn)) {
			entity.setPrjLgcSn(Long.valueOf(prjLgcSn));
		}
		if (StringUtils.isNotEmpty(srplsAtm)) {
			entity.setSrplsAtm(new BigDecimal(srplsAtm));
		}
		if (StringUtils.isNotEmpty(txnAmt)) {
			entity.setTxnAmt(new BigDecimal(txnAmt));
		}
		entity.setTxnTp(MrgnManageInfoConstant.MRGN_MGT_INFO_TXN_TP_DEDUCT);
		entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
		entity.setCrtTm(new Date());
		entity.setIsValid(BaseConstant.IS_YESNO_NO); // 有效
		entity.setIsDel(BaseConstant.IS_YESNO_NO); // 未删除
		
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "保证金还款信息添加成功!");
		
		try {
			Long mainId = this.mrgnManageInfoService.addMrgnDetail(entity);
			//新增保证金坐扣
			JSONArray deductOprtJsonArray = JSONArray.parseArray(deductOprtArray);
			List<MrgnTkOffInfoVO> mrgnTkOffInfoList=new ArrayList<MrgnTkOffInfoVO>();
			if (deductOprtJsonArray.size() > 0) {
				for (int i = 0; i < deductOprtJsonArray.size(); i++) {
					JSONObject deductOprtJSONObject = deductOprtJsonArray.getJSONObject(i);
					if(deductOprtJSONObject != null && deductOprtJSONObject.containsKey("__id")) {
						deductOprtJSONObject.remove("__id");
					}
					MrgnTkOffInfoVO mrgnTkOffInfoVO = JSONObject.toJavaObject(deductOprtJSONObject, MrgnTkOffInfoVO.class);
					if(mrgnTkOffInfoVO.getThsRepyAmt()==null||mrgnTkOffInfoVO.getThsRepyAmt()==new BigDecimal(0)){
						continue;
					}
					if(mrgnTkOffInfoVO.getFeeClsNm()=="本金"){
						mrgnTkOffInfoVO.setFeeClsCd(1);
					}
					if(mrgnTkOffInfoVO.getFeeClsNm()=="利息"){
						mrgnTkOffInfoVO.setFeeClsCd(2);
					}
					mrgnTkOffInfoVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
					mrgnTkOffInfoVO.setCrtTm(new Date());
					mrgnTkOffInfoVO.setIsValid(BaseConstant.IS_YESNO_YES); // 有效
					mrgnTkOffInfoVO.setIsDel(BaseConstant.IS_YESNO_NO); // 未删除
					mrgnTkOffInfoVO.setMgtLgcSn(mainId);
					mrgnTkOffInfoList.add(mrgnTkOffInfoVO);
				}
			}
			this.mrgnManageInfoService.addMrgnTkOffInfo(mrgnTkOffInfoList);
			result.setData(mainId);
			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setInfo("保证金还款信息添加成功!");
			BaseLogUtils.info(logger, "addDeductOprt", "新增数据 ,项目逻辑序号:id["+prjLgcSn+"]. ID:"+mainId, CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "addDeductOprt", "新增数据失败,项目逻辑序号:id["+prjLgcSn+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("新增失败!");
			BaseLogUtils.error(logger, "addDeductOprt", "新增数据失败,项目逻辑序号:id["+prjLgcSn+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	/**
	 * 保证金坐扣-修改
	 */
	@RequestMapping(value = "/updateDeductOprt.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object updateDeductOprt(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		String txnAmt = request.getParameter("txnAmt"); 
	    String srplsAtm = request.getParameter("srplsAtm"); 
	    String prjLgcSn = request.getParameter("prjLgcSn"); 
		String deductOprtArray = request.getParameter("deductOprtArray"); 
		String mgtLgcSn = request.getParameter("mgtLgcSn"); 
		
		//新增保证金坐扣
		JSONArray deductOprtJsonArray = JSONArray.parseArray(deductOprtArray);
		List<MrgnTkOffInfoVO> mrgnTkOffInfoList=new ArrayList<MrgnTkOffInfoVO>();
		if (deductOprtJsonArray.size() > 0) {
			for (int i = 0; i < deductOprtJsonArray.size(); i++) {
				MrgnTkOffInfoVO mrgnTkOffInfoVO = JSONObject.toJavaObject(deductOprtJsonArray.getJSONObject(i), MrgnTkOffInfoVO.class);
				if(mrgnTkOffInfoVO.getThsRepyAmt()==null||mrgnTkOffInfoVO.getThsRepyAmt()==new BigDecimal(0)){
					continue;
				}
				if(mrgnTkOffInfoVO.getFeeClsNm()=="本金"){
					mrgnTkOffInfoVO.setFeeClsCd(1);
				}
				if(mrgnTkOffInfoVO.getFeeClsNm()=="利息"){
					mrgnTkOffInfoVO.setFeeClsCd(2);
				}
				mrgnTkOffInfoVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				mrgnTkOffInfoVO.setCrtTm(new Date());
				mrgnTkOffInfoVO.setIsValid(BaseConstant.IS_YESNO_YES); // 有效
				mrgnTkOffInfoVO.setIsDel(BaseConstant.IS_YESNO_NO); // 未删除
				if (StringUtils.isNotEmpty(mgtLgcSn)) {
					mrgnTkOffInfoVO.setMgtLgcSn(Long.valueOf(mgtLgcSn));
				}
				mrgnTkOffInfoList.add(mrgnTkOffInfoVO);
			}
		}
		
	    //添加保证金流水信息
		MrgnDetailInfoVO entity=new MrgnDetailInfoVO();
		if (StringUtils.isNotEmpty(prjLgcSn)) {
			entity.setPrjLgcSn(Long.valueOf(prjLgcSn));
		}
		if (StringUtils.isNotEmpty(srplsAtm)) {
			entity.setSrplsAtm(new BigDecimal(srplsAtm));
		}
		if (StringUtils.isNotEmpty(txnAmt)) {
			entity.setTxnAmt(new BigDecimal(txnAmt));
		}
		if (StringUtils.isNotEmpty(mgtLgcSn)) {
			entity.setId(Long.valueOf(mgtLgcSn));
		}
		entity.setTxnTp(MrgnManageInfoConstant.MRGN_MGT_INFO_TXN_TP_DEDUCT);
		entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
		entity.setCrtTm(new Date());
		entity.setIsValid(BaseConstant.IS_YESNO_NO); // 有效
		entity.setIsDel(BaseConstant.IS_YESNO_NO); // 未删除
		
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "保证金还款信息修改成功!");
		
		try {
			this.mrgnManageInfoService.updateMrgnDetail(entity);
			this.mrgnManageInfoService.updateMrgnTkOffInfo(mrgnTkOffInfoList);
			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setInfo("保证金还款信息修改成功!");
			BaseLogUtils.info(logger, "updateDeductOprt", "修改数据 ,项目逻辑序号:id["+prjLgcSn+"].", CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "updateDeductOprt", "修改数据失败,项目逻辑序号:id["+prjLgcSn+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("修改失败!");
			BaseLogUtils.error(logger, "updateDeductOprt", "修改数据失败,项目逻辑序号:id["+prjLgcSn+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	/**
	 * 保证金坐扣前-查询项目是否正在还款
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getRepayMainStByPrjId.do", method = { RequestMethod.GET})
	@ResponseBody
	public DataResultVO getRepayMainStByPrjId(HttpServletRequest request, HttpServletResponse response) {
		String prjLgcSn = request.getParameter("prjLgcSn");
		if (StringUtils.isEmpty(prjLgcSn)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "项目信息编号不能为空!");
		}
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "获取成功!");
		try {
			FncPrjPaySchMVO fncPrjPaySchMVO = this.mrgnManageInfoService.selectRepayMainStByPrjId(Long.valueOf(prjLgcSn));
			result.setData(fncPrjPaySchMVO);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getMrgnReturnInfoByLgcId", "获取数据失败,项目逻辑编号["+prjLgcSn+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误!");
			BaseLogUtils.error(logger, "getMrgnReturnInfoByLgcId", "获取数据失败,项目逻辑编号["+prjLgcSn+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	/**
	 * 将List转换为List<KeyValueVO>
	 * @param list
	 * @param strCode
	 * @param strName
	 * @param idNm
	 * @return
	 */
    public <T> List<KeyValueVO> getKvListT(List<T> list,String strCode,String strName){
         
         List<KeyValueVO> kvList = new ArrayList<>();
         try {
             if (CollectionUtils.isNotEmpty(list)) {
                   T entityOne=list.get(0);
                   Class<?> Obj = entityOne.getClass();
                   PropertyDescriptor pdCode = null;
                   PropertyDescriptor pdName = null;
                  try {
                       pdCode = new PropertyDescriptor(strCode, Obj);
                       pdName = new PropertyDescriptor(strName, Obj); 
                  } catch (IntrospectionException e1) {
                       e1.printStackTrace();
                  }
                  
                 Method getCodeMethod = pdCode.getReadMethod();//获得get方法
                 Method getNameMethod = pdName.getReadMethod();//获得get方法
                  for (T entity : list) {
                       String codeS=getCodeMethod.invoke(entity).toString();
                       String nameS=getNameMethod.invoke(entity).toString();
                       KeyValueVO keyValueVO = new KeyValueVO();
                       keyValueVO.setKey(nameS);
                       keyValueVO.setValue(codeS);
                       kvList.add(keyValueVO);
                  }
             }
         } catch (BaseException e) {
             System.out.println("error");
             } catch (IllegalAccessException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
         } catch (IllegalArgumentException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
         } catch (InvocationTargetException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
         }
         return kvList;
    }
    
    /**
	 * 将List转换为List<KeyValueVO>
	 * @param list
	 * @param strCode
	 * @param strName
	 * @param idNm
	 * @return
	 */
    public MrgnDetailInfoVO getBankInfo(MrgnDetailInfoVO mrgnDetailInfoVO){
		// 保证金收款帐户信息
		if (StringUtils.isNotEmpty(mrgnDetailInfoVO.getRecvAcc())) {
			BankAccInfoDTO bankAccInfoDTO = bankAccInfoApiService.selectById(mrgnDetailInfoVO.getRecvAcc(), CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
			if(bankAccInfoDTO!=null){
				BankAccInfoVO bankAccInfoVo =BankAccInfoVOTransform.toVO(bankAccInfoDTO);
				mrgnDetailInfoVO.setReAccNm(bankAccInfoVo.getAccNm());
				mrgnDetailInfoVO.setReBanAccNm(bankAccInfoVo.getBanAccNm());
				mrgnDetailInfoVO.setReAccNum(bankAccInfoVo.getAccNum());
			}
	    }			
		// 我司付款帐户信息
		if (StringUtils.isNotEmpty(mrgnDetailInfoVO.getPayAcc())) {
				SysBkAccInfoVO bkAccInfoVO = sysBkAccInfoService.selectById(mrgnDetailInfoVO.getPayAcc());
				if(bkAccInfoVO!=null){
					mrgnDetailInfoVO.setPayAccNm(bkAccInfoVO.getBkNm());
					mrgnDetailInfoVO.setPayAccNum(bkAccInfoVO.getAccNo());
					mrgnDetailInfoVO.setPayBanAccNm(bkAccInfoVO.getAccNm());
				}
		}
		return mrgnDetailInfoVO;
    }

	
	/**
	 * 加载金融机构收款户选择框
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/initRecvList.do" ,method = {RequestMethod.GET})
	public @ResponseBody  List<BankAccInfoDTO>  initRecvList(HttpServletRequest request, HttpServletResponse response) {
		String instid = request.getParameter("insId");
		if (StringUtils.isEmpty(instid)) {
			return null;
		}
		BankAccInfoQueryCondition bankAccInfoQC = new BankAccInfoQueryCondition();
		bankAccInfoQC.setFundRasInsId(Long.parseLong(instid));
		bankAccInfoQC.setSysCd(CurrentThreadContext.getCurrentSysCd());
		bankAccInfoQC.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
		List<BankAccInfoDTO> banklst = bankAccInfoApiService.searchList(bankAccInfoQC);
		
		return banklst;
	}
	
	/**
	 * 加载狮桥支付账户选择框
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/initPayList.do" ,method = {RequestMethod.GET})
	public @ResponseBody  List<SysBkAccInfoVO>  initPayList(HttpServletRequest request, HttpServletResponse response) {
		SysBkAccInfoQueryCondition qc = new SysBkAccInfoQueryCondition();
		qc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
		List<SysBkAccInfoVO> bkAccList = sysBkAccInfoService.searchList(qc);
		return bkAccList;
	}
    
    //保证金坐扣-发起流程
	@RequestMapping(value = "/addMrgnDeductFlowStart.do", method = { RequestMethod.POST})
	@ResponseBody
	public Object addMrgnDeductFlowStart(HttpServletRequest request, HttpServletResponse response) {

		String id = request.getParameter("id");
		String procDefKey = request.getParameter("procDefKey");
		String procDefId = request.getParameter("procDefId");
		String procNo = request.getParameter("procNo");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "addFlowStart", SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY, CurrentThreadContext.getCurrentUserName());
			return result;
		}
		FlowStartParamsDTO flowStartParamsDTO = new FlowStartParamsDTO();
		try {
			//流程定义Key和流程定义ID 如果是前端选择的则直接指定,如果不是则系统自动按照配置选择唯一一条记录,如果找不到会提示
			flowStartParamsDTO.setAudStsCd(FlowConstant.FLOW_CHECK_STATE_IN);
			flowStartParamsDTO.setWordsInTheTable(MrgnManageInfoConstant.REF_FLOW_S_H_MRGND);
			flowStartParamsDTO.setProcDefKey(procDefKey);
			flowStartParamsDTO.setProcDefId(procDefId);
			flowStartParamsDTO.setProcNo(procNo);
			flowStartParamsDTO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
			flowStartParamsDTO.setBusinessKey(id);
			flowStartParamsDTO.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
			flowStartParamsDTO.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
			flowStartParamsDTO.setSysCd(CurrentThreadContext.getCurrentSysCd());
			flowStartParamsDTO.setInitiator(String.valueOf(CurrentThreadContext.getCurrentUserId()));
			flowStartParamsDTO.setInitiatorNm(CurrentThreadContext.getCurrentUserName());
			flowStartParamsDTO.setProcPriority(FlowConstant.FLOW_PRIORITY_EMERGENCY);//优先级,可以在界面定义
			flowStartParamsDTO.setBusinessType(MrgnManageInfoConstant.FLOW_BUSINESS_TYP_MRGN_DEDUCT_INFO);//确认用不用
			flowStartParamsDTO.setModelTyp(MrgnManageInfoConstant.FLOW_MODEL_TYP_MRGND);//需要和流程配置中的模块类型匹配
			flowStartParamsDTO.setSearchDesc(CurrentThreadContext.getCurrentOrgDispNm()+"、授信流程大字段参数未定!");
			DataResultDTO dataResultDTO = this.mrgnManageInfoService.addMrgnDeductFlowStart(flowStartParamsDTO);
			if(!dataResultDTO.getCode().equals(BaseExceptionConstant.BASE_EXCEPTION_CODE_200)){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_OPERATE_FAIL);
			}
			if(null!=dataResultDTO && null!=dataResultDTO.getData()){
				FlowStartedDTO flowStartedDTO = (FlowStartedDTO) dataResultDTO.getData();
				if(StringUtils.isNotBlank(flowStartedDTO.getNextOperUsrNames())){
	                result.setInfo("下一个节点操作人:"+flowStartedDTO.getNextOperUsrNames());
	            }else{
	                result.setInfo("流程发起成功");
	            }
			}
			/*if(null != dataResultDTO.getInfo() && StringUtils.isNotEmpty(dataResultDTO.getInfo())){
				StringBuffer info = new StringBuffer();
				info.append(result.getInfo());
				info.append(SysLogConstant.LOG_FLOW_NEXT_TASK_OPERATOR);
				info.append(dataResultDTO.getInfo());//操作人
				result.setInfo(info.toString());
			}*/
		} catch (BaseException e) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(e.getMessage());
			BaseLogUtils.info(logger, "addFlowStart", "错误[BaseException],请求参数：" + JSONObject.toJSONString(flowStartParamsDTO) + " -ERROR:" +e.getMessage(), CurrentThreadContext.getCurrentUserName());
			e.getStackTrace();
		} catch (Exception e) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "addFlowStart", "错误[Exception],请求参数：" + JSONObject.toJSONString(flowStartParamsDTO) + " -ERROR:" +e.getMessage(), CurrentThreadContext.getCurrentUserName());
			e.getStackTrace();
		}
		return result;
	}
	
	//保证金退回-发起流程
	@RequestMapping(value = "/addMrgnReturnFlowStart.do", method = { RequestMethod.POST})
	@ResponseBody
	public Object addMrgnReturnFlowStart(HttpServletRequest request, HttpServletResponse response) {

		String id = request.getParameter("id");
		String procDefKey = request.getParameter("procDefKey");
		String procDefId = request.getParameter("procDefId");
		String procNo = request.getParameter("procNo");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "addFlowStart", SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY, CurrentThreadContext.getCurrentUserName());
			return result;
		}
		FlowStartParamsDTO flowStartParamsDTO = new FlowStartParamsDTO();
		try {
			//流程定义Key和流程定义ID 如果是前端选择的则直接指定,如果不是则系统自动按照配置选择唯一一条记录,如果找不到会提示
			flowStartParamsDTO.setAudStsCd(FlowConstant.FLOW_CHECK_STATE_IN);
			flowStartParamsDTO.setWordsInTheTable(MrgnManageInfoConstant.REF_FLOW_S_H_MRGNR);
			flowStartParamsDTO.setProcDefKey(procDefKey);
			flowStartParamsDTO.setProcDefId(procDefId);
			flowStartParamsDTO.setProcNo(procNo);
			flowStartParamsDTO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
			flowStartParamsDTO.setBusinessKey(id);
			flowStartParamsDTO.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
			flowStartParamsDTO.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
			flowStartParamsDTO.setSysCd(CurrentThreadContext.getCurrentSysCd());
			flowStartParamsDTO.setInitiator(String.valueOf(CurrentThreadContext.getCurrentUserId()));
			flowStartParamsDTO.setInitiatorNm(CurrentThreadContext.getCurrentUserName());
			flowStartParamsDTO.setProcPriority(FlowConstant.FLOW_PRIORITY_EMERGENCY);//优先级,可以在界面定义
			flowStartParamsDTO.setBusinessType(MrgnManageInfoConstant.FLOW_BUSINESS_TYP_MRGN_RETURN_INFO);//确认用不用
			flowStartParamsDTO.setModelTyp(MrgnManageInfoConstant.FLOW_MODEL_TYP_MRGNR);//需要和流程配置中的模块类型匹配
			flowStartParamsDTO.setSearchDesc(CurrentThreadContext.getCurrentOrgDispNm()+"、授信流程大字段参数未定!");
			DataResultDTO dataResultDTO = this.mrgnManageInfoService.addMrgnReturnFlowStart(flowStartParamsDTO);
			if(!dataResultDTO.getCode().equals(BaseExceptionConstant.BASE_EXCEPTION_CODE_200)){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_OPERATE_FAIL);
			}
			if(null!=dataResultDTO && null!=dataResultDTO.getData()){
				FlowStartedDTO flowStartedDTO = (FlowStartedDTO) dataResultDTO.getData();
				if(StringUtils.isNotBlank(flowStartedDTO.getNextOperUsrNames())){
	                result.setInfo("下一个节点操作人:"+flowStartedDTO.getNextOperUsrNames());
	            }else{
	                result.setInfo("流程发起成功");
	            }
			}
			/*if(null != dataResultDTO.getInfo() && StringUtils.isNotEmpty(dataResultDTO.getInfo())){
				StringBuffer info = new StringBuffer();
				info.append(result.getInfo());
				info.append(SysLogConstant.LOG_FLOW_NEXT_TASK_OPERATOR);
				info.append(dataResultDTO.getInfo());//操作人
				result.setInfo(info.toString());
			}*/
		} catch (BaseException e) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(e.getMessage());
			BaseLogUtils.info(logger, "addFlowStart", "错误[BaseException],请求参数：" + JSONObject.toJSONString(flowStartParamsDTO) + " -ERROR:" +e.getMessage(), CurrentThreadContext.getCurrentUserName());
			e.getStackTrace();
		} catch (Exception e) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "addFlowStart", "错误[Exception],请求参数：" + JSONObject.toJSONString(flowStartParamsDTO) + " -ERROR:" +e.getMessage(), CurrentThreadContext.getCurrentUserName());
			e.getStackTrace();
		}
		return result;
	}

}

